package edu.gatech.app4args;

import edu.gatech.app4args.resources.Globals;
import edu.gatech.app4args.utils.CompendiumNode;
import edu.gatech.app4args.utils.SlideData;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 * Controls the slide content panels. Reconfigures the visible sections based
 * on the selected slide type.
 *
 * @author Andrew Lunsford
 */
public class SlideContentPanel extends JPanel{
	/** A SlideContentSection for the title section */
	private SlideContentSection title;
	/** A SlideContentSection for the first sub section */
	private SlideContentSection section1;
	/** A SlideContentSection for the second sub section */
	private SlideContentSection section2;

	/** A reference to the Main class */
	private Main ref;
	/** The index of the current slide */
	private int slideNumber;

	/**
	 * Creates a new SlideContentPanel, sets the layout, and initializes all
	 * three SlideContentSections. Sets the reference to the Main class.
	 *
	 * @param ref a reference to the Main class
	 */
	public SlideContentPanel(Main ref){
		//this.setName("slideContentPanel");
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		title = new SlideContentSection(this, Globals.section_title);
		section1 = new SlideContentSection(this, Globals.section_content1);
		section2 = new SlideContentSection(this, Globals.section_content2);

		this.ref = ref;
	}

	/**
	 * Sets up the SlideContentPanel, displaying the correct sections for the
	 * selected slide type.
	 *
	 * @param slidetype the type of slide to setup the content area for
	 */
	private void setup(int slidetype){
		this.removeAll();
		this.addSection(title,"TITLE",2);
		switch(slidetype){
			case Globals.sType_title:
				this.addSection(section1,"SUBTITLE",2);
				break;
			case Globals.sType_standard:
				this.addSection(section1,"TEXTAREA",5);
				break;
			case Globals.sType_two_content:
				this.addSection(section1,"TEXTAREA 1",5);
				this.addSection(section2,"TEXTAREA 2",5);
				break;
		}
	}

	/**
	 * Loads the SlideContentSections with the given SlideData object and
	 * reconfigures the view.
	 *
	 * @param slide the SlideData object to load
	 * @param slideNumber the index of the SlideData object
	 */
	public void load(SlideData slide, int slideNumber){
		this.slideNumber = slideNumber;
		this.setup(slide.getType());
		title.load(slide.getTitle().getContent());
		section1.load(slide.getContent1().getContent());
		section2.load(slide.getContent2().getContent());
		this.revalidate();
		this.repaint();
	}

	/**
	 * An intermediate method that allows a SlideContentSection to add content
	 * to the current SlideData object held in the Main class
	 *
	 * @param sectionNumber the section to add content to
	 * @param key the key of the CompendiumNode the retrieve the content from
	 * @return true if the content was successfully added to the SlideData object
	 */
	public boolean addSlideData(int sectionNumber, String key){
		System.out.println("SCP> adding section: "+sectionNumber);

		if(ref.addSlideData(slideNumber, sectionNumber, key)){
			this.revalidate();
			this.repaint();
			return true;
		}
		return false;

	}

	/**
	 * An intermediate method that allows a SlideDataSection to retrieve a
	 * CompendiumNode from the Main class.
	 *
	 * @param key the key of the CompendiumNode to get
	 * @return the CompendiumNode that corresponds to the specified key.
	 */
	public CompendiumNode getNode(String key){
		return ref.getNode(key);
	}

	/**
	 * Sets up a SlideContentSection with the given name and number of rows
	 * and adds it to the panel.
	 *
	 * @param section the SlideContentSection to setup and add
	 * @param name the name of the SlideContentSection
	 * @param rows the number of rows to display
	 */
	private void addSection(SlideContentSection section, String name, int rows){
		section.setup(name, rows);
		this.add(section);
	}

	/**
	 * Prints out the current data contained in the SlideContentSections
	 * controlled by the panel.
	 */
	public void printString(){
		String out = "=========================================\n";
		out += "SlideContentPanel current data:\n";
		out += "\t--------------";
		System.out.println(out);
		title.printString();
		System.out.println("\t--------------");
		section1.printString();
		System.out.println("\t--------------");
		section2.printString();
		System.out.println("\t--------------");
		System.out.println("=========================================\n");
	}
}
